package com.all.play.myapplication.adapter;

import android.content.Context;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.all.play.myapplication.R;
import com.all.play.myapplication.bean.MediaLocalVideoItem;
import com.all.play.myapplication.utils.Utils;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/3/29.
 */

public class LocalVideoAdapter extends BaseAdapter {
    private Context context;
    private List<MediaLocalVideoItem> medialist;
    private final Utils utils;

    public LocalVideoAdapter(Context context, List<MediaLocalVideoItem> medialist) {
        this.context=context;
        this.medialist=medialist;
        utils = new Utils();

    }

    @Override
    public int getCount() {
        return medialist==null?0:medialist.size();
    }

    @Override
    public Object getItem(int position) {
        return medialist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=View.inflate(parent.getContext(), R.layout.item_local_video,null);
            viewHolder.image_icon=convertView.findViewById(R.id.image_video_icon);
            viewHolder.tv_name=convertView.findViewById(R.id.tv_video_name);
            viewHolder.tv_time=convertView.findViewById(R.id.tv_video_time);
            viewHolder.tv_size=convertView.findViewById(R.id.tv_video_size);

            convertView.setTag(viewHolder);

        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }

        MediaLocalVideoItem videoItem = medialist.get(position);
        viewHolder.tv_name.setText(videoItem.getName());
        viewHolder.tv_size.setText(Formatter.formatShortFileSize(context,videoItem.getSize()));
        viewHolder.tv_time.setText(utils.stringForTime((int) videoItem.getDuration()));
        return convertView;
    }

    static class ViewHolder{
        ImageView image_icon;
        TextView tv_name,tv_time,tv_size;

    }
}
